//
//  LotteryViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/17.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "LotteryViewController.h"
#import "LotteryModel.h"
#import "LotteryListViewController.h"
#import "LotteryViewModel.h"
#import "LotteryListCell.h"

#import "LotteryTypeModel.h"

@interface LotteryViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic,strong) LotteryViewModel * lotteryModel;

@property (nonatomic,strong) UITableView * tableView;


@end

@implementation LotteryViewController
-(LotteryViewModel *) lotteryModel{
    if (!_lotteryModel) {
        _lotteryModel = [[LotteryViewModel alloc] init];
    }
    return _lotteryModel;
}
-(UITableView *) tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT - 49) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];

    /**
     网络请求
     */
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(lotteryNetWork)];
    [self.tableView.mj_header beginRefreshing];
}

-(void) lotteryNetWork{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    [[self.lotteryModel getLotteryTypeRequestParameter] subscribeNext:^(id x) {
        
        [self.tableView reloadData];
        [MXUtility hideMBProgress:self.view];
        [self.tableView.mj_header endRefreshing];
        
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        [self.tableView.mj_header endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.lotteryModel.userLotteryList count];
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str = @"cell";
    LotteryListCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"LotteryListCell" owner:self options:nil] lastObject];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell setlotteryType:[self.lotteryModel.userLotteryList objectAtIndex:indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LotteryListViewController * list = [[LotteryListViewController alloc] init];
    
    LotteryTypeModel * model = [self.lotteryModel.userLotteryList objectAtIndex:indexPath.row];
    
    [list setCpId:model.typeId];
    [list setCpImg:model.typeimgUrl];
    [list setCpName:model.typelotteryTypeName];
    
    
    [self.navigationController pushViewController:list animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0);
    cell.alpha = 0.0;
    cell.layer.transform = transform3D;
    cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置；默认为中心点(0.5, 0.5)

    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDuration:0.5];
    cell.alpha = 1.0;
    cell.layer.transform = CATransform3DIdentity;
    CGRect rect = cell.frame;
    rect.origin.x = 0.0;
    cell.frame = rect;
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
